package com.cdy.common.uitl;

import com.cdy.common.property.CommonProperty;
import lombok.extern.slf4j.Slf4j;

import java.util.BitSet;

@Slf4j
public class BitSetUtil {


    /**
     * 分段是否完成(部分完成)
     *
     * @param sectionBegin 分段开始的位置
     * @param begin        开始的位置
     * @param bitBegin     bitmap开始下标
     * @param bitLength    bitmap范围
     * @param bitSet       bitmap
     * @return
     */
    public static Boolean sectionFinish(Long sectionBegin, Long begin, Integer bitBegin, Integer bitLength, BitSet bitSet) {
        long sectionSize = CommonProperty.SmallSectionLength;
        int currentBit = MathUtil.divide(correctLong(begin - sectionBegin), sectionSize) - 1;
        bitSet.set(currentBit);
        return sectionFinish(currentBit, bitBegin, bitLength, bitSet);
    }

    public static Boolean sectionFinish(Integer currentBit, Integer bitBegin, Integer bitLength, BitSet bitSet) {
        int nextBit = bitSet.nextClearBit(bitBegin);
        log.info("position {} begin {} end {} next {} ", currentBit, bitBegin, bitBegin + bitLength - 1, nextBit);
        return (nextBit - bitBegin) >= bitLength;
    }

    public static long correctLong(long l) {
        if (l % CommonProperty.SmallSectionLength == 0) {
            return l + 1;
        }
        return l;
    }
}
